//
//  TLRGifView.m
//  iOSBaseFramework
//
//  Created by user on 2020/6/18.
//  Copyright © 2020 com.taylor. All rights reserved.
//

#import "TRGifTool.h"
#import "NSString+tool.h"



@interface TRGifTool()
    @property (nonatomic, strong)UIView *gifContentView;
    @property (nonatomic, assign)CGImageSourceRef gif;
    @property (nonatomic, strong)NSDictionary *gifDic;
    @property (nonatomic, assign)size_t index;
    @property (nonatomic, assign)size_t count;
    @property (nonatomic, strong)NSTimer *timer;
@end

@implementation TRGifTool

+ (UIWebView *)createGifByWebView:(NSString *)fileName{
    if ([fileName isEmptyString]) {
        NSLog(@"fileName 为Null");
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile:path];
    
    float screenWidth = [[UIScreen mainScreen] bounds].size.width;
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(screenWidth*0.5-15, 15, 30, 30)];
    webView.scalesPageToFit = YES;
    [webView loadData:gifData MIMEType:@"image/gif" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]];
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    return webView;
}

+(WKWebView *)createGifByWKWebView:(NSString *)fileName {
    if ([fileName isEmptyString]) {
        NSLog(@"fileName 为Null");
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile:path];
    
    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
    WKWebView *wkwebVIew = [[WKWebView alloc] initWithFrame:CGRectMake(screenWidth * 0.5 - 15, 15, 30, 30)];
    [wkwebVIew loadData:gifData MIMEType:@"image/gif" characterEncodingName:@"" baseURL:[NSURL URLWithString:@""]];
    wkwebVIew.backgroundColor = [UIColor clearColor];
    wkwebVIew.opaque = NO;
    return wkwebVIew;
}

- (void)addGifWithName:(NSString *)gifName toView:(UIView *)view {
    self.gifContentView = view;
    [self createGif:gifName];
}

- (void)createGif:(NSString *)name {
    NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjects:[NSNumber numberWithInt:0] forKeys:(NSString *)kCGImagePropertyGIFLoopCount];
    _gifDic = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString*)kCGImagePropertyGIFDictionary];
    
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]];
    _gif = CGImageSourceCreateWithData((CFDataRef)gif, (CFDictionaryRef)_gifDic);
    _count = CGImageSourceGetCount(_gif);
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
    [_timer fire];
}

- (void)startLoading
{
    _index ++;
    _index = _index%_count;
    CGImageRef ref = CGImageSourceCreateImageAtIndex(_gif, _index, (CFDictionaryRef)_gifDic);
    self.gifContentView.layer.contents = (__bridge id)ref;
    CFRelease(ref);
}

- (void)stopGif
{
    [_timer invalidate];
    _timer = nil;
}


- (void)dealloc {
    if (_gif) {
        CFRelease(_gif);
    }
}

@end
